// Depends on tencentcloud-sdk-nodejs version 4.0.3 or higher
// eslint-disable-next-line @typescript-eslint/no-var-requires
const tencentcloud = require("tencentcloud-sdk-nodejs");
import { secretId, secretKey } from '../../config/secret';
import { UUID36 } from '../index';
import { uploadFileBase64Service, uploadFileService } from '../tenxunCos';

const TtsClient = tencentcloud.tts.v20190823.Client;

const clientConfig = {
  credential: {
    SecretId: secretId,
    SecretKey: secretKey,
  },
  region: "ap-guangzhou",
  profile: {
    httpProfile: {
      endpoint: "tts.tencentcloudapi.com",
    },
  },
};

const client = new TtsClient(clientConfig);

// 获取语音合成文件
export function getVoiceDataService(text: string) {
  return new Promise((resolve, reject) => {
    client.TextToVoice({
      Text: text,
      SessionId: UUID36(10),
    }).then(
      (data: IAnyObject) => {
        const dataBuffer = Buffer.from(data.Audio, 'base64');
        uploadFileBase64Service({ name: 'yuyin.mp3', data: dataBuffer}).then(res => {
          resolve(res);
        })
      },
      (err: IAnyObject) => {
        console.error("error", err);
        reject(err);
      }
    );
  })
}